window.onload = function () {
    axios.defaults.withCredentials = true;
    //登录接口
    //获取信息
    let kh_login = document.getElementById("kh_login")
    let kh_mycount = document.getElementById("kh_mycount")
    let kh_mypassword = document.getElementById("kh_mypassword")
    let kh_myyzm = document.getElementById("kh_myyzm")

    //获取验证码
    getCode();
    // 刷新验证码
    $('.kh_yzmup').click(function () {
        kh_myyzm.value = ''
        getCode();
    })
    $(kh_login).on('click', function (event) {
        event.preventDefault()
        if(kh_mycount.value && kh_mypassword.value &&kh_myyzm.value){
        axios.post('https://elm.cangdu.org/v2/login', {
                username: kh_mycount.value,
                password: kh_mypassword.value,
                captcha_code: kh_myyzm.value
            }).then(res => {

            console.log(res);
            if(res.data.status==0){
                Toast('warning',res.data.message);
            }
            if(res.data.username==kh_mycount.value){
             window.location.href=`/pages/my/my.html`  
            }
          
            axios.get('https://elm.cangdu.org/v1/user').then(res => {
                console.log(res);
            
            })
        })
    }
    else if(kh_mycount.value==''){
        // alert('请填写账号')
        Toast('warning','请填写账号');
    }
    else if(kh_mypassword.value==''){
        Toast('warning','请填写密码');
    }
    else if(kh_myyzm.value==''){
        Toast('warning','请填写验证码');
    }
    })
    //重置密码
    let kh_forget = document.getElementById("kh_forget")
    $('#kh_forget').click(function () {
        window.location.href = `/pages/forget/forget.html`
    })

}
// 工具函数-- 获取验证码
function getCode() {

    axios.post('https://elm.cangdu.org/v1/captchas').then(data => {
        console.log(data)
        var lists = data.data.code;
        // var dom = `<img class="kh_yzmpic" src="${lists}">`
        $('.kh_yzmup').html(`<img class="kh_yzmpic" src="${lists}">
        <span class='kh_yzma' style="font-size: 0.2rem;">看不清<p
          style="color: #3b95e9;">换一张</p>
      </span>
        `) //验证码图片渲染到了界面

    })




}
// 展示密码

function move(){
    var box=document.getElementById("kh_circle");
    $('#kh_circle').addClass('kh_backtoyl')
    var tuoyuan = document.getElementById("kh_tuoyuan");
    box.style.left=6.7+'rem';
    tuoyuan.style.backgroundColor = "#4cd964";
    $('#kh_mypassword').attr('type','text')
  }
  function back(){
    var box=document.getElementById("kh_circle");
    $('#kh_circle').addClass('kh_backtoyl')
    var tuoyuan = document.getElementById("kh_tuoyuan");
    box.style.left=6+'rem';
    tuoyuan.style.backgroundColor = "#ccc";
    $('#kh_mypassword').attr('type','password')
  }
  
  
  
  
// 工具函数--获取登录接口
function login() {
    $().axios({
        url: 'https://elm.cangdu.org/v2/login',
        method: 'post',
        data: {
            username: kh_mycount.value,
            password: kh_mypassword.value,
            captcha_code: kh_myyzm.value
        }
    }).then(res => {
        console.log(res);
    })
}